<?php
namespace Platform\Controller;
use Think\Controller;
class GoodsController extends CommController {
    public function index(){
        $db = M('goods');
        $n =M("business");
        $bid = $_GET['bid'];
        if($_GET['bid']){
            $bb = $n->field('name')->where("id = $bid")->select();
            foreach($bb as $key=>$value){
                $map[$key]['b.name'] = array('like','%'.$value['name'].'%');
                $map[$key]['g.name'] = array('like','%'.$_GET['selname'].'%');
            }
        }else{
            $map[]['g.name'] = array('like','%'.$_GET['selname'].'%');
        }
        $count = $db->where('del =1')->count();
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $define = ['g.*','t.name tname','p.img','count(p.id) num','b.name bname'];
        $list = $db->alias('g')->
        join('type t ON g.tid = t.id')->
        join('goods_pic p ON g.id = p.gid')->
        join('business b ON g.bid = b.id')->
        field($define)
            ->group('g.id')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->where($map)
             ->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);
        $this->display();
    }
    public function pic()
    {
        ini_set('max_execution_time', '0');
        ini_set('post_max_size','128M');
        ini_set('upload_max_filesize','128M');
        ini_set('max_input_time','0');
        $db = M('goods_pic');
        if(IS_POST) {
            switch ($_GET['t']) {
                case 'add':
                    $file = $this->upload();
                    if(!$file) die('cuowu');
                    $data['img'] = $file['file']['savepath'].$file['file']['savename'];
                    $data['gid'] = $_REQUEST['id'];
                    $data['time'] = time();
                    $result = $db->data($data)->add();
                    if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
                        alert('新增成功');
                        echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
                    } else {
                        unlink('.'.$data['img']);
                            //错误页面的默认跳转页面是返回前一页，通常不需要设置
                        alert('新增失败');
                        echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
                    }
                    break;
                case 'update':
                    $file = $this->upload();
                    if(!$file) die('cuowu ');
                    unlink('.'.$db->find($_POST['id'])['path']);
                    $data['img'] = $file['file']['savepath'].$file['file']['savename'];
                    $data['id'] = $_POST['id'];
                    $result = $db->data($data)->save();
                    if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
                        alert('修改成功');
                        echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
                    } else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置
                        alert('修改失败');
                        echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
                    }
                    break;

                default:
                    # code...
                    break;
            }
        }else{
          $where = [
             'gid' => $_GET['id'],
             'del'=>1
          ];
            $count = $db->where('del =1')->count();
            $Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show = $Page->show();// 分页显示输出
            $this->assign('page',$show);// 赋值分页输出
            $this->assign('list',$db->where($where)->limit($Page->firstRow.','.$Page->listRows)->select());
            $this->display();
        }
    }
    public function picdel()
    {
        $db = M('goods_pic');
        $data['del'] = 0;
        $data['deltime']=time();
        $list = $db->where('id='.$_GET['id'])->save($data);
        if ($list)
        {
            alert('删除成功');
            echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }else{
            alert('删除失败');
            echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }
    }
//     public function add()
//     {
//         if (IS_POST)
//         {
//             $pic = $this->upload();
//             if($pic){
//                 if($pic['info']) $_POST['info'] = $pic['info']['savepath'].$pic['info']['savename'];
//                 $_POST['time'] = time();
//                 $db = M('goods');
//                 $type = M('type');
//                 M()->startTrans();//开启事务
//                 $key = M('keyword');
//                 $in = $db->data($_POST)->add();
//                 $kin = true; //验证关键字添加
//                 if($pic['img'] && $in != 0) {
//                     $data['img'] = $pic['img']['savepath'] . $pic['img']['savename'];
//                     $data['gid'] = $in;
//                     $data['time'] = time();
//                     $keyword = explode(' ',$_POST['keyword']);
//                     if($keyword)
//                     {
//                         foreach ($keyword as $k => $v)
//                         {
//                             $data['key'] = $v;
//                             $data['gid'] = $in;
//                             $data['time'] = $_POST['time'];
//                             if (!$key->data($data)->add()){
//                                 M()->rollback();
//                                 alert('添加失败');
//                                 $kin = false;
//                             }
//                         }
//                     }
//                     $ii = M('goods_pic')->data($data)->add();
//                     if (!$ii) alert('商品图片添加失败');
//                 }

//                 if($in > 0 && $kin && $ii)
//                 {
//                     M()->commit();
//                     alert('添加成功');
//                     echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
//                 }else{
//                     M()->rollback();
//                     unlink('.'.$pic['img']['savepath'] . $pic['img']['savename']);
//                     unlink('.'.$pic['info']['savepath'] . $pic['info']['savename']);
//                     alert('添加失败');
//                     echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
//                 }
//             }
//             alert('没有图片上传');
// //            echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
//         }else{
//             $type = M('type')->alias('a')->
//             order('concat(a.path,a.id)')->join('type b ON b.pid = a.id','left')->
//             field('a.*,b.pid')->group('a.id')->select();

//             $this->assign('type',$type);
//             $this->display();
//         }
//     }
    // public function edit()
    // {
    //     $db = M('goods');
    //     if(IS_POST) {
    //         $pic = upload();
    //         $kin = true;
    //         if($pic['info']) $_POST['info'] = $pic['info']['savepath'].$pic['info']['savename'];
    //         $keyword = explode(' ',$_POST['keyword']);
    //         if($keyword) {
    //             $key = M('keyword');
    //             $key->where(['gid'=>$_POST['id']])->delete();
    //             foreach ($keyword as $k => $v)
    //             {
    //                 $data['key'] = $v;
    //                 $data['gid'] = $_POST['id'];
    //                 $data['time'] = time();
    //                 if($key->data($data)->add())
    //                     $kin = false;
    //             }
    //         }
    //         if($db->save($_POST) || $kin)
    //         {
    //             alert('修改成功');
    //             echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
    //         }else{
    //             alert('修改成功');
    //             echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
    //         }
    //     }else{
    //         $list = M('type')->alias('a')->join('type b ON a.id = b.pid','left')->order('concat(a.path,a.id)')->field('a.*,b.id pid')->select();
    //         $this->assign('type',$list);
    //         $keyword = M('keyword')->where(['gid'=>$_GET['id']])->field('key')->select();
    //         foreach ($keyword as $v)
    //             $klist[] = $v['key'];
    //         $this->assign('keyword',implode(' ',$klist));
    //         $this->assign('info',$db->find($_GET['id']));
    //          $this->assign('list',M('business')->field("id,name")->select());
           
    //         $this->display();
    //     }
    // }
    public function qj()
    {
        $db = M('goods');
        $info = $db->find($_POST['id']);
        if (!$info){
            echo "{'state':3,'message':'没有这条数据'}";
            return;
        }
        if($info['state'] == 1)
        {
            $_POST['state'] = 2;
            if (!$db->save($_POST))
                echo json_encode(['state'=>'3','message'=>'修改失败']);
            else
                echo json_encode(['state'=>'2','message'=>'修改成功']);
        }else{
            $_POST['state'] = 1;
            if (!$db->save($_POST))
                echo json_encode(['state'=>'3','message'=>'修改失败']);
            else
                echo json_encode(['state'=>'1','message'=>'修改成功']);
        }
    }
    public function get($pid = '')
    {
        $where = '';
        if($pid != '') $where['pid'] = $pid;
        $list = M('type')->order('concat(pid,id)')->where($where)->select();
        return $list;
    }
    public function del()
    {
        M()->startTrans();//开启事务
        $db = M('goods');
        $info = $db->field('info')->find($_GET['id']);
        $piclist = M('goods_pic')->where(['gid'=>$_GET['id']])->field('img')->select();

        $g = $db->delete($_GET['id']);
        $p = M('goods_pic')->where(['gid'=>$_GET['id']])->delete();
        $k = M('keyword')->where(['gid'=>$_GET['id']])->delete();
        if ($g && ($p || $k))
        {
            M()->commit();
            foreach ($piclist as $v) {
                unlink('.'.$v['img']);
            }
            alert('删除成功');
            echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }else{
            M()->rollback();
            alert('删除失败');
//            echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }


    }
     public function picqj()
    {
        $db = M('goods_pic');
        $info = $db->find($_POST['id']);
        if (!$info){
            echo "{'state':3,'message':'没有这条数据'}";
            return;
        }
        if($info['state'] == 1)
        {
            $_POST['state'] = 2;
            if (!$db->save($_POST))
                echo json_encode(['state'=>'3','message'=>'修改失败']);
            else
                echo json_encode(['state'=>'2','message'=>'修改成功']);
        }else{
            $_POST['state'] = 1;
            if (!$db->save($_POST))
                echo json_encode(['state'=>'3','message'=>'修改失败']);
            else
                echo json_encode(['state'=>'1','message'=>'修改成功']);
        }
    }


}